package com.example.ioc.context;

import com.example.common.utils.Statistics;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * {@link org.springframework.boot.SpringApplication#addInitializers(ApplicationContextInitializer[])}
 */
@Slf4j
public class LApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private static volatile LApplicationContextInitializer instance;

    private LApplicationContextInitializer() {
        Statistics.logInfo(log);
    }

    public static LApplicationContextInitializer getInstance() {
        if (instance == null) {
            synchronized (LApplicationContextInitializer.class) {
                if (instance == null) {
                    instance = new LApplicationContextInitializer();
                }
            }
        }
        return instance;
    }

    @Override
    public void initialize(@NonNull ConfigurableApplicationContext applicationContext) {
        Statistics.logInfo(log, "initialize", applicationContext);
        applicationContext.addBeanFactoryPostProcessor(LBeanDefinitionRegistryPostProcessor.getInstance());
        applicationContext.addBeanFactoryPostProcessor(LBeanFactoryPostProcessor.getInstance());
        applicationContext.addApplicationListener(LApplicationListener.getInstance());
        applicationContext.addProtocolResolver(LProtocolResolver.getInstance());
    }
}
